@page "/DataValidation"
@using BlazorDatasheet.Core.Interfaces
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.Core.Validation

<PageTitle>Data validation</PageTitle>

<h1>Data validation</h1>

<p>
    Below data validation is used on the cells.
</p>
<p>Column 1 uses a NumberValidator with IsStrict = true.</p>
<p>Column 2 uses a Number validator with IsStrict = false.</p>
<p>Column 3 uses a SourceValidator with IsStrict = false.</p>
<p>Column 4 uses a custom validator that ensures the string contains the word foo, with IsStrict = false</p>

<Datasheet Sheet="sheet"></Datasheet>

@code {

    private Sheet sheet;

    protected override void OnInitialized()
    {
        sheet = new Sheet(1, 4);

        sheet.Cells[0, 2].Type = "select";
        sheet.Validators.Add(0, 0, new NumberValidator(true));
        sheet.Validators.Add(0, 1, new NumberValidator(false));
        sheet.Validators.Add(0, 2, new SourceValidator(new List<string>() { "Item 1", "Item 2", "Item 3" }, false));
        sheet.Validators.Add(0, 3, new CustomValidator("foo", false));
        base.OnInitialized();
    }

    private class CustomValidator : IDataValidator
    {
        private readonly string _word;
        public bool IsStrict { get; }
        public string Message => $"Should contain the word {_word}";

        public CustomValidator(string word, bool isStrict)
        {
            IsStrict = isStrict;
            _word = word;
        }

        public bool IsValid(object? value)
        {
            if (value == null)
                return false;
            var str = value.ToString();
            return str.Contains(_word);
        }
    }

}